package com.brainyx.sample;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.brainyxsample.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class Pulltorefresh extends Activity{

	PullToRefreshListView listViewMain;
	ExamAdapter listAdapter;
	int basecount = 20;
	int pluscount = 10;
	int maxcount = 100; 
	
	ArrayList<String>arraylist = new ArrayList<String>();
	boolean hasMoreData ;
	int page = 1;
	Handler handler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// 테스트.
		setContentView(R.layout.pulltolistview);
		init();
		view();
	}
	
	private void init(){
		
		listViewMain = (PullToRefreshListView) findViewById(R.id.listViewMain);
		listViewMain.setMode(Mode.PULL_FROM_END);
		listAdapter = new ExamAdapter(Pulltorefresh.this,listViewMain);

		listViewMain.setAdapter(listAdapter);
		
	
	}
	
	private void view(){
		
		for(int i = arraylist.size() ; i < basecount * page; i++){
			arraylist.add(String.valueOf(i));
		}
		
		if (arraylist.size() == basecount * page && arraylist.size() < 100) {
			listViewMain.setMode(Mode.BOTH);
			hasMoreData = true;
		} else {
			//listViewMain.setMode(Mode.PULL_FROM_START);
			hasMoreData = false;
			removeListViewFooter();
		}
		
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				listViewMain.onRefreshComplete();
				listAdapter.notifyDataSetChanged();
			}
		}, 1000);
		
	
		
		//listViewMain.setAdapter(listAdapter);
	}
	
	
	/**
	 *  더보기 footer 삭제 하는 함수
	 */
	void removeListViewFooter() {
		AsyncTask<Void, Void, Void> getDataWorker = new AsyncTask<Void, Void, Void>() {
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				try {
				} catch (Exception e) {
				}
			}

			@Override
			protected Void doInBackground(Void... params) {
				try {
				} catch (Exception e) {
				}

				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				try {
					listViewMain.setMode(Mode.PULL_FROM_START);
				} catch (Exception e) {
				} finally {
				}
				super.onPostExecute(result);
			}
		};
		getDataWorker.execute();
	}
	
	public class ExamAdapter extends ArrayAdapter<String> {

		public Context mContext;
		public ArrayList<String> info;
		public int mResource;

		public ExamAdapter(Context context, PullToRefreshListView listview) {
			super(context, R.layout.listview_item, arraylist);
			// TODO Auto-generated constructor stub

			this.mContext = context;
			info = arraylist;

			listview.setOnRefreshListener(new OnRefreshListener<ListView>() {
				@Override
				public void onRefresh(PullToRefreshBase<ListView> refreshView) {
					String label = DateUtils.formatDateTime(mContext,
							System.currentTimeMillis(),
							DateUtils.FORMAT_SHOW_TIME
									| DateUtils.FORMAT_SHOW_DATE
									| DateUtils.FORMAT_ABBREV_ALL);
					// Update the LastUpdatedLabel
					refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(
							label);
					if (refreshView.getCurrentMode() == Mode.PULL_FROM_START) {
						page = 1;
						arraylist.clear();
						view();
					} else if (refreshView.getCurrentMode() == Mode.PULL_FROM_END) {
						if (hasMoreData) {
							page = page + 1;
							view();
						}
					}
					// Add Data..
				}
			});
		}

		public class ViewHolder {
			TextView  itemtextview;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null) {
				LayoutInflater layoutInflater = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = layoutInflater.inflate(R.layout.listview_item, null);
				holder = new ViewHolder();
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			if (info != null && info.size() > 0) {

				String  data = info.get(position);
				holder.itemtextview = (TextView) convertView.findViewById(R.id.itemtextview);
				holder.itemtextview.setText(data);
			}
			return convertView;
		}
	}
}
